<?php

namespace App\Library\Logic;

use App\Library\Requests\ProductOrder\ProductOrderCreateRequest;
use App\Models\CartModel;
use Illuminate\Database\Eloquent\Collection;

class CartLogic extends CartModel
{
    /**
     * 列表
     * @return Collection|array
     *
     */
    public function list(): Collection|array
    {
        return self::query()
            ->leftJoin('shop_product', 'shop_product.product_id', '=', 'cart.object_id')
            ->leftJoin('shop_product_specs', 'shop_product_specs.ps_id', '=', 'cart.spec_id')
            ->where('cart.user_id', USERID)
            ->get([
                'cart.*',
                'shop_product.product_id',
                'shop_product.product_category_id_1',
                'shop_product.product_status',
                'shop_product.product_name',
                'product_avatar',
                'ps_avatar',
                'ps_name',
                'product_stock_empty',
                'ps_buy_limit',
                'product_special_start_time',
                'product_special_end_time',
                'sub_price',
                'ps_total_stock',
                'ps_usable_stock',
                'ps_buy_limit',
                'ps_price',
                'ps_price_underlined'
            ]);
    }

    /**
     * clearBySpecIds
     * @param ProductOrderCreateRequest $request
     * @return void
     */
    public static function clearBySpecIds(ProductOrderCreateRequest $request): void
    {
        if ($request->isCart !== true) return;
        self::query()->where('user_id', USERID)->where('object_type', 'shop_product')
            ->whereIn('spec_id', array_keys($request->specArr))
            ->delete();
    }
}
